package com.pet_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pet_service.entity.HomeServiceType;
import com.pet_service.entity.PetInfo;
import com.pet_service.entity.User;
import com.pet_service.entity.UserAddress;
import com.pet_service.result.Result;
import com.pet_service.service.HomeServiceTypeService;
import com.pet_service.service.PetInfoService;
import com.pet_service.service.UserAddressService;
import com.pet_service.service.UserService;
import com.pet_service.upload.AvatarUploadUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 邹全伸
 * @version 1.0
 * @date 2025-10-05 16:57
 */
@Slf4j
@RestController
@Tag(name = "客户模块")
@RequestMapping("/app/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private PetInfoService petInfoService;

    @Resource
    private UserAddressService userAddressService;

    @Resource
    private HomeServiceTypeService homeServiceTypeService;
    // 根据手机号码查询用户信息
    @GetMapping("/getInfo/{phone}")
    @Operation(summary = "获取顾客信息")
    public Result getInfo(@PathVariable("phone") String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        User user = userService.getOne(queryWrapper);
        return Result.ok(user);
    }

    // 获取用户宠物列表
    @GetMapping("/{userId}/pets")
    @Operation(summary = "获取用户宠物列表")
    public Result<List<PetInfo>> getPets(@PathVariable("userId") Integer userId) {
        QueryWrapper<PetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return Result.ok(petInfoService.list(queryWrapper));
    }

    // 获取用户地址列表
    @GetMapping("/{userId}/addresses")
    @Operation(summary = "获取用户地址列表")
    public Result<List<UserAddress>> getAddresses(@PathVariable("userId") Integer userId) {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return Result.ok(userAddressService.list(queryWrapper));
    }

    // 根据id获取服务类型详情
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取服务类型详情")
    public Result<HomeServiceType> getServiceType(@PathVariable("id") Integer id) {
        HomeServiceType serviceType = homeServiceTypeService.getById(id);
        if (serviceType != null) {
            return Result.ok(serviceType);
        } else {
            return Result.fail(500,"服务类型不存在");
        }
    }

    // 获取全部服务类型列表
    @GetMapping("/service-types")
    @Operation(summary = "获取全部服务类型列表")
    public Result<List<HomeServiceType>> getAllServiceTypes() {
        List<HomeServiceType> serviceTypes = homeServiceTypeService.list();
        return Result.ok(serviceTypes);
    }

    //修改头像
    @PostMapping("/editAvatar")
    public Result editImage(@RequestBody User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("avatar_url", user.getAvatar_url());
        updateWrapper.eq("id", user.getId());
        boolean flag = userService.update(updateWrapper);
        return flag ? Result.ok("头像更新成功!") : Result.fail("头像更新失败!");
    }
}

